package com.yonyou.gmmc.service.shopping.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.constants.ResultMsgConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmmc.common.annotation.ParamValid;
import org.marketing.common.Response;
import com.yonyou.gmmc.service.shopping.constant.ShoppingConstant;
import com.yonyou.gmmc.service.shopping.model.ProdCategoryTreePo;
import com.yonyou.gmmc.service.shopping.model.TmFixCode;
import com.yonyou.gmmc.service.shopping.model.TmTpProductCategory;
import com.yonyou.gmmc.service.shopping.service.ProductMsgService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 商城的订单相关API
 * @author BENJAMIN
 *
 */
@RestController
@RequestMapping(value=ShoppingConstant.BASE_PATH + "/" + ShoppingConstant.VERSION)
@Api("商品管理相关API")
@CrossOrigin
public class ProductMsgController {

    private Logger logger = Logger.getLogger(ProductMsgController.class);
	
	@Autowired
	private ProductMsgService productMsgService;
	
	/**
    * 固定代码列表
    * @author Administrator
    * @date 2017年4月17日
    * @return
    */  
	@RequestMapping(value="/getTypeList",method = RequestMethod.GET)
    @ApiOperation(value="搜索条件下拉列表",notes="搜索条件下拉列表")
	@ParamValid
    public Response<List<TmFixCode>> getTypeList(@RequestParam Integer codeType) {
		logger.info("ProductController.queryTypeList==>start...");
        try {
			List<TmFixCode> list = productMsgService.queryTypeList(codeType);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, list, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常",e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}finally {
			logger.info("ProductController.queryTypeList==>end...");
		}	
    }
	/**
    * 获取产品规格
    * @author Administrator
    * @date 2017年4月17日
    * @return
    */  
	@RequestMapping(value="/productNorms",method = RequestMethod.GET)
    @ApiOperation(value="获取产品规格",notes="获取产品规格")
	@ParamValid
    public Response<List<Map<String,Object>>> getProductNorms(@RequestParam String inputSku) {
		logger.info("ProductController.getProductNorms==>start...");
        try {
        	List<Map<String,Object>> list = productMsgService.getProductNorms(inputSku);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, list, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常",e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}finally {
			logger.info("ProductController.getProductNorms==>end...");
		}	
    }
	
	@RequestMapping(value="/validProductCategorys",method = RequestMethod.GET)
    @ApiOperation(value="获取商品有效分类",notes="获取商品有效分类")
	public Response getValidProductCategorys() {
		try {
			Map<String,Object> paramsMap = new HashMap<>();
			paramsMap.put("state", 30071001);
			List<TmTpProductCategory> validProductCategorysList = productMsgService.getProductCategorys(paramsMap);
			
			Map<String,ProdCategoryTreePo> tempMap = new HashMap<String, ProdCategoryTreePo>();
        	
    		for(int n = 0;n<validProductCategorysList.size();n++){
    			ProdCategoryTreePo categoryTree=new ProdCategoryTreePo();
    			categoryTree.setCatId(validProductCategorysList.get(n).getCatId());
    			categoryTree.setCategoryId(validProductCategorysList.get(n).getCategoryId());
    			categoryTree.setName(validProductCategorysList.get(n).getName());
    			categoryTree.setParentId(validProductCategorysList.get(n).getParentId());
    			tempMap.put(validProductCategorysList.get(n).getCatId().toString(), categoryTree);
    		}

            for(Map.Entry<String,ProdCategoryTreePo> entry:tempMap.entrySet()){
            	if(0!=entry.getValue().getParentId()){
            		ProdCategoryTreePo rt = tempMap.get(entry.getValue().getParentId().toString());
	                if(rt != null){
	                    tempMap.get(entry.getValue().getParentId().toString()).getChildren().add(entry.getValue());
	
	                }
            	}
            }
            
            List<Map<String,Object>> responseList = new ArrayList<>();
            
            for(Map.Entry<String,ProdCategoryTreePo> entry:tempMap.entrySet()){
            	Map<String,Object> firstMap = new HashMap<>(); 
            	if(0==entry.getValue().getParentId()){
            		//resultList.add(entry.getValue());
            		firstMap.put("id", entry.getValue().getCatId());
            		firstMap.put("name", entry.getValue().getName());
            		
            		if(entry.getValue().getChildren().size()>0){
            			List<Map<String,Object>> firstList = new ArrayList<>();
            			
            			for(int i = 0;i<entry.getValue().getChildren().size();i++){
            				Map<String,Object> secondMap = new HashMap<String,Object>(); 
            				secondMap.put("id", entry.getValue().getChildren().get(i).getCatId());
            				secondMap.put("name", entry.getValue().getChildren().get(i).getName());
            				List<Map<String,Object>> secondList = new ArrayList<Map<String,Object>>();
            				if(entry.getValue().getChildren().get(i).getChildren().size()>0){
            					
            					for(int j = 0;j<entry.getValue().getChildren().get(i).getChildren().size();j++){
            						Map<String,Object> thirdMap = new HashMap<String,Object>(); 
            						thirdMap.put("id", entry.getValue().getChildren().get(i).getChildren().get(j).getCatId());
            						thirdMap.put("name", entry.getValue().getChildren().get(i).getChildren().get(j).getName());
            						if(entry.getValue().getChildren().get(i).getChildren().get(j).getChildren().size()>0){
            							thirdMap.put("child", entry.getValue().getChildren().get(i).getChildren().get(j).getChildren());
            						}
            						secondList.add(thirdMap);
            					}
            					secondMap.put("child", secondList);
            					
            				}
            				firstList.add(secondMap);
            			}
            			firstMap.put("child", firstList);
            			
            		}
            		responseList.add(firstMap);
            	}
            }
			
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, responseList, "");
		} catch (Exception e) {
			logger.error("获取商品有效分类====="+e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}finally{
			logger.info("ProductController.validProductCategorys==>end");
		}
	}
	
	
}

